/**
 * Created by liuyanjie on 2019/6/13.
 */

// 获取数组中某个相同字段的个数
const sameNum = (arr,same)=> {
	let number = ''; 
    for(let i= 0, l = arr.length; i< l; i++){ 
        let item = arr[i]; 
        if(item == same){
        	number++;
        }
    } 
    return number; 
}

//数组去重
const deleteSame = (arr)=> {
	let newArr = [];
	for(let i = 0,length = arr.length;i<length;i++){
		if(arr.indexOf(arr[i]) <= i){
			newArr.push(arr[i]);
		}
	}
	return newArr;
}

//返回同类数据合并后的数组
const mergeData = (arr,sameAttr)=> {
	let newArr = [];
    if(arr && arr.length > 0){
        for (let i = 0; i < arr.length; i++){
            let thisData = arr[i];
            let nextData = arr[i+1];
            if(nextData){
                if(thisData[sameAttr] !== nextData[sameAttr]){
                    newArr.push(thisData[sameAttr]);
                }
            }else{
                newArr.push(thisData[sameAttr]);
            } 
        }
    }
    return newArr;
}

const tranString = (arr)=> {
    if(arr instanceof Array){
        return JSON.stringify(arr).replace(/[\[]/,'').replace(/[\]]/,'').replace(/"/g,'');   //数组转换为x,x,x格式字符串 
    }else{
        return arr;   //返回原值
    }
}


export {
	sameNum,
	deleteSame,
	mergeData,
    tranString
}